/*****************************************************************************
 *   Copyright (C) 2008 John Schember <john@nachtimwald.com>                 *
 *                                                                           *
 *   This file is part of niwbillmanager.                                    *
 *                                                                           *
 *   niwbillmanager is free software: you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as          *
 *   published by the Free Software Foundation, either version 3 of the      *
 *   License, or (at your option) any later version.                         *
 *                                                                           *
 *   niwbillmanager is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with niwbillmanager. If not, see                                  *
 *   <http://www.gnu.org/licenses/>.                                         *
 *****************************************************************************/

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QCloseEvent>
#include <QMainWindow>
#include <QMenu>

#include "ui_mainwindow.h"

#include <niwbillmanager.h>
#include "pluginmanager.h"

class PluginManager;

class MainWindow : public QMainWindow, private Ui_MainWindow
{
    Q_OBJECT

    public:
        MainWindow();
        ~MainWindow();

        QMenu *getToolsMenu();

    signals:
        void uiUpdate();
        void billItemsChange();
        void billItemSelect(QTreeWidgetItem *selectedItem);
        void tagsChange();
        void undoRedoUpdate();
        void titleUpdate();

    protected:
        void closeEvent(QCloseEvent *event);
        QMenu *createPopupMenu();

    private slots:
        void on_actionNew_triggered();
        void on_actionOpen_triggered();
        void on_actionSave_triggered();
        void on_actionSave_As_triggered();
        void on_actionUndo_triggered();
        void on_actionRedo_triggered();
        void on_actionAdd_Item_triggered();
        void on_actionEdit_Item_triggered();
        void on_actionDelete_Item_triggered();
        void on_actionSearch_triggered();
        void on_actionPreferences_triggered();
        void on_actionToolbar_triggered();
        void on_actionText_On_Toolbar_triggered();
        void on_actionPlugin_Browser_triggered();
        void on_actionAbout_triggered();
        void on_actionAbout_Qt_triggered();

        void on_filterList_itemSelectionChanged();
        void on_tagList_itemSelectionChanged();
        void on_itemList_itemSelectionChanged();
        void on_itemList_itemActivated();

        void uiUpdated();
        void billItemsChanged();
        void billItemSelected(QTreeWidgetItem *selectedItem);
        void tagsChanged();
        void undoRedoUpdated();
        void titleUpdated();

    private:
        void connectSignals();
        void clearCollectionInformation();
        void save();
        void saveAs();
        /**
         * Checks if a save should happen. Shows a message box asking the user
         * if they would like to save.
         * @return True if the action should continue. False if it should be
         * stopped.
         */
        bool checkSaveAndShouldContinueAction();

        /**
         * Sets various settings with QSettings.
         * Reads and sets a number of settings relating to the size, position
         * and structure of the window.
         */
        void loadSettings();
        /**
         * Saves various settings with QSettings.
         * Reads and writes a number of settings relating to the size, position
         * and strcutre of the window.
        */
        void saveSettings();
        /**
         * Returns a , separated string of the sizes of a splitter.
         * @param splitter The splitter to get the sizes of.
         */
        QString getSplitterSize(QSplitter *splitter);
        /**
         * Sets the size of a splitter from a , separated string of sizes.
         * @param splitter The splitter to resize
         * @param size Comma separated string of sizes
         */
        void setSplitterSize(QSplitter *splitter, QString size);

        NiwBillManager *m_lib;
        PluginManager *m_pluginManager;

        QString m_searchQuery;

        QString m_collectionLocation;
        QString m_collectionUsername;
        QString m_collectionLoginname;
        QString m_collectionPassword;
};

#endif /* MAINWINDOW_H */
